
--[[
    通过webservice发送过车数量
    流程：
    1.查询mysql,找到所有的路口编号、路口名称、以及路口类型(卡口或电警)
    2.通过路口取redis中的一些流量信息
    3.打包成webservice发送
key: lkbh_gc_numbyhour            ----每小时过车总量
     lkbh_gc_smacar_numbyhour      ----小车流量
     lkbh_gc_bigcar_numbyhour     ----大车流量
     lkbh_gc_natcar_numbyhour     ----本地车流量
     lkbh_gc_waicar_numbyhour     ----外地车流量
                                  ----其它流量
--]]

local db_method = require ("gcs.db.method")
local red_method = require ("gcs.list.method")
local http        = require ("resty.http")
local utils      =  require ("gcs.utils")
local shell       = require("resty.shell") 
local cjson                    = require "cjson"
local config_table             = ngx.shared.gcs_config   --声明ngx.shared.gcs_config
local model     = "send_hour_num"
local tb_insert=table.insert

--[[
local status, out, err = shell.execute("date -d -1hour +'%Y-%m-%d %H:00:00'")
if not status then
    utils.gcs_log("error",model..",resty_shell err")
    ngx.exit(200)
end--]]
local time =1111 --string.gsub(out,'%c','')
local hour =14--string.sub(time,12,13)

----------------定义函数
------组装xml函数
local function load_to_xml(tb_data)
    
    local tb_xml = {}
  tb_xml[1] = [[<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.ehl.com">
   <soapenv:Header/>
   <soapenv:Body>
      <web:receiveVolume>
         <web:TGSID>]] ..tb_data.lkbh
   tb_xml[2] = [[</web:TGSID><web:DIRECTION>]] ..tb_data.detec       
  tb_xml[3] = [[</web:DIRECTION><web:DataTime>]] ..tb_data.time 
  tb_xml[4] = [[</web:DataTime><web:VOLUME>]] ..tb_data.num
  tb_xml[5] = [[</web:VOLUME><web:VOLUME1>]] ..tb_data.smanum
  tb_xml[6] = [[</web:VOLUME1><web:VOLUME2>]] ..tb_data.bignum
  tb_xml[7] = [[</web:VOLUME2><web:VOLUME3>]] ..tb_data.natnum
  tb_xml[8] = [[</web:VOLUME3><web:VOLUME4>]] ..tb_data.wainum
  tb_xml[9] = [[</web:VOLUME4><web:VOLUME5>]] ..tb_data.othernum
  tb_xml[10] = [[</web:VOLUME5><web:TGSSTATE>]] ..tb_data.state
  tb_xml[11] = [[</web:TGSSTATE><web:REMARK>]] ..tb_data.remark
  tb_xml[12] = [[</web:REMARK>]]
  tb_xml[13] =[[
  </web:receiveVolume>
   </soapenv:Body>
</soapenv:Envelope>]]

    return table.concat(tb_xml)
end

-------发送xml函数
local function webservice(uri,body)
    local res = {}
    local hc = http:new()
    local url = uri
    res.ok, res.code, res.headers, res.status, res.body  = hc:request {
      url = url,
      timeout = 5000,
      method = "POST", -- POST or GET
      -- add post content-type and cookie
      headers = {["Content-Type"] = "application/soap+xml; charset=utf-8" },
      body = body
       }
    if not res.ok then
        return nil,"webservice is err,url:"..url
    end
    return res,nil
end

local dic={"N","W","S","E"}

--------1.查出所有的路口

local tb_data,err = db_method.query("select distinct c_kkmc,c_lkbh from base_sbxx")
if not tb_data then
    utils.gcs_log("error",model..",mysql_query err:"..err)
    ngx.exit(200)
end



--------2.准备发送数据,发送

for _,v in pairs(tb_data) do
--[[
     lkbh_gc_smacarE_numbyhour      ----小车流量
     lkbh_gc_bigcarE_numbyhour     ----大车流量
     lkbh_gc_natcarE_numbyhour     ----本地车流量
     lkbh_gc_waicarE_numbyhour     ----外地车流量
--]]
    for _,d in pairs(dic) do
        local tb_send_data = {}
        local tb_numbyhour = red_method.get_data(v.c_lkbh.."_gc_" ..d .."numbyhour_"..hour)
        local tb_smanumbyhour = red_method.get_data(v.c_lkbh.."_gc_2car" ..d .."_numbyhour_"..hour)
        local tb_bignumbyhour = red_method.get_data(v.c_lkbh.."_gc_1car" ..d .."_numbyhour_"..hour)
        local tb_natnumbyhour = red_method.get_data(v.c_lkbh.."_gc_natcar" ..d .."_numbyhour_"..hour)
        local tb_wainumbyhour = red_method.get_data(v.c_lkbh.."_gc_waicar" ..d .."_numbyhour_"..hour)
        local tb_othernumbyhour = red_method.get_data(v.c_lkbh.."_gc_othercar" ..d .."_numbyhour_"..hour)
        tb_send_data.lkbh = v.c_lkbh
        tb_send_data.lkmc = v.c_kkmc
        local lx
        if tonumber(tb_data.lx) == 0 then
            lx = "TGS"          ---卡口
        else
            lx = "PMS"          ---电警
        end
        tb_send_data.lx = lx or ""
        tb_send_data.time = time       ---时间
        tb_send_data.detec = d     ---方向
        tb_send_data.num = tb_numbyhour[v.c_lkbh.."_gc_" ..d .."numbyhour_" ..hour] or 0
        tb_send_data.smanum = tb_smanumbyhour[v.c_lkbh.."_gc_2car" ..d .."_numbyhour_" ..hour] or 0
        tb_send_data.bignum = tb_bignumbyhour[v.c_lkbh.."_gc_1car" ..d .."_numbyhour_" ..hour] or 0
        tb_send_data.natnum = tb_natnumbyhour[v.c_lkbh.."_gc_natcar" ..d .."_numbyhour_" ..hour] or 0
        tb_send_data.wainum = tb_wainumbyhour[v.c_lkbh.."_gc_waicar" ..d .."_numbyhour_" ..hour]  or 0
        tb_send_data.othernum= tb_othernumbyhour[v.c_lkbh.."_gc_othercar" ..d .."_numbyhour_" ..hour] or 0
        tb_send_data.state = 1
        tb_send_data.remark = "测试"     ----备注
        db_method.save_data("numbyhour",tb_send_data)     ---先插数据库,再发送
        utils.gcs_log("error","tb_send" ..cjson.encode(tb_send_data))
        local body = load_to_xml(tb_send_data)
        utils.gcs_log("error",model.."xml:"..cjson.encode(body)) 
        local uri = "http://192.168.1.141:13201/vss/2.0/workflow/test"
        local res,err = webservice(uri,body)
        if not res then
            utils.gcs_log("error",model..",webservice err:"..err)   --发送失败记录日志
        end
        if true then
		ngx.exit(200)
	end
        ngx.sleep(10)             ----10秒钟发送一次
    end
end



